module LolModelFormat
    module Md2
    	
    	class Md2File < BinData::Record
    	
			#//
			#//	Extracted from anorms.h - header file
			#//    	    	
			Anorms = [
				[ -0.525731,  0.000000,  0.850651], 
				[ -0.442863,  0.238856,  0.864188], 
				[ -0.295242,  0.000000,  0.955423], 
				[ -0.309017,  0.500000,  0.809017], 
				[ -0.162460,  0.262866,  0.951056], 
				[  0.000000,  0.000000,  1.000000], 
				[  0.000000,  0.850651,  0.525731], 
				[ -0.147621,  0.716567,  0.681718], 
				[  0.147621,  0.716567,  0.681718], 
				[  0.000000,  0.525731,  0.850651], 
				[  0.309017,  0.500000,  0.809017], 
				[  0.525731,  0.000000,  0.850651], 
				[  0.295242,  0.000000,  0.955423], 
				[  0.442863,  0.238856,  0.864188], 
				[  0.162460,  0.262866,  0.951056], 
				[ -0.681718,  0.147621,  0.716567], 
				[ -0.809017,  0.309017,  0.500000], 
				[ -0.587785,  0.425325,  0.688191], 
				[ -0.850651,  0.525731,  0.000000], 
				[ -0.864188,  0.442863,  0.238856], 
				[ -0.716567,  0.681718,  0.147621], 
				[ -0.688191,  0.587785,  0.425325], 
				[ -0.500000,  0.809017,  0.309017], 
				[ -0.238856,  0.864188,  0.442863], 
				[ -0.425325,  0.688191,  0.587785], 
				[ -0.716567,  0.681718, -0.147621], 
				[ -0.500000,  0.809017, -0.309017], 
				[ -0.525731,  0.850651,  0.000000], 
				[  0.000000,  0.850651, -0.525731], 
				[ -0.238856,  0.864188, -0.442863], 
				[  0.000000,  0.955423, -0.295242], 
				[ -0.262866,  0.951056, -0.162460], 
				[  0.000000,  1.000000,  0.000000], 
				[  0.000000,  0.955423,  0.295242], 
				[ -0.262866,  0.951056,  0.162460], 
				[  0.238856,  0.864188,  0.442863], 
				[  0.262866,  0.951056,  0.162460], 
				[  0.500000,  0.809017,  0.309017], 
				[  0.238856,  0.864188, -0.442863], 
				[  0.262866,  0.951056, -0.162460], 
				[  0.500000,  0.809017, -0.309017], 
				[  0.850651,  0.525731,  0.000000], 
				[  0.716567,  0.681718,  0.147621], 
				[  0.716567,  0.681718, -0.147621], 
				[  0.525731,  0.850651,  0.000000], 
				[  0.425325,  0.688191,  0.587785], 
				[  0.864188,  0.442863,  0.238856], 
				[  0.688191,  0.587785,  0.425325], 
				[  0.809017,  0.309017,  0.500000], 
				[  0.681718,  0.147621,  0.716567], 
				[  0.587785,  0.425325,  0.688191], 
				[  0.955423,  0.295242,  0.000000], 
				[  1.000000,  0.000000,  0.000000], 
				[  0.951056,  0.162460,  0.262866], 
				[  0.850651, -0.525731,  0.000000], 
				[  0.955423, -0.295242,  0.000000], 
				[  0.864188, -0.442863,  0.238856], 
				[  0.951056, -0.162460,  0.262866], 
				[  0.809017, -0.309017,  0.500000], 
				[  0.681718, -0.147621,  0.716567], 
				[  0.850651,  0.000000,  0.525731], 
				[  0.864188,  0.442863, -0.238856], 
				[  0.809017,  0.309017, -0.500000], 
				[  0.951056,  0.162460, -0.262866], 
				[  0.525731,  0.000000, -0.850651], 
				[  0.681718,  0.147621, -0.716567], 
				[  0.681718, -0.147621, -0.716567], 
				[  0.850651,  0.000000, -0.525731], 
				[  0.809017, -0.309017, -0.500000], 
				[  0.864188, -0.442863, -0.238856], 
				[  0.951056, -0.162460, -0.262866], 
				[  0.147621,  0.716567, -0.681718], 
				[  0.309017,  0.500000, -0.809017], 
				[  0.425325,  0.688191, -0.587785], 
				[  0.442863,  0.238856, -0.864188], 
				[  0.587785,  0.425325, -0.688191], 
				[  0.688191,  0.587785, -0.425325], 
				[ -0.147621,  0.716567, -0.681718], 
				[ -0.309017,  0.500000, -0.809017], 
				[  0.000000,  0.525731, -0.850651], 
				[ -0.525731,  0.000000, -0.850651], 
				[ -0.442863,  0.238856, -0.864188], 
				[ -0.295242,  0.000000, -0.955423], 
				[ -0.162460,  0.262866, -0.951056], 
				[  0.000000,  0.000000, -1.000000], 
				[  0.295242,  0.000000, -0.955423], 
				[  0.162460,  0.262866, -0.951056], 
				[ -0.442863, -0.238856, -0.864188], 
				[ -0.309017, -0.500000, -0.809017], 
				[ -0.162460, -0.262866, -0.951056], 
				[  0.000000, -0.850651, -0.525731], 
				[ -0.147621, -0.716567, -0.681718], 
				[  0.147621, -0.716567, -0.681718], 
				[  0.000000, -0.525731, -0.850651], 
				[  0.309017, -0.500000, -0.809017], 
				[  0.442863, -0.238856, -0.864188], 
				[  0.162460, -0.262866, -0.951056], 
				[  0.238856, -0.864188, -0.442863], 
				[  0.500000, -0.809017, -0.309017], 
				[  0.425325, -0.688191, -0.587785], 
				[  0.716567, -0.681718, -0.147621], 
				[  0.688191, -0.587785, -0.425325], 
				[  0.587785, -0.425325, -0.688191], 
				[  0.000000, -0.955423, -0.295242], 
				[  0.000000, -1.000000,  0.000000], 
				[  0.262866, -0.951056, -0.162460], 
				[  0.000000, -0.850651,  0.525731], 
				[  0.000000, -0.955423,  0.295242], 
				[  0.238856, -0.864188,  0.442863], 
				[  0.262866, -0.951056,  0.162460], 
				[  0.500000, -0.809017,  0.309017], 
				[  0.716567, -0.681718,  0.147621], 
				[  0.525731, -0.850651,  0.000000], 
				[ -0.238856, -0.864188, -0.442863], 
				[ -0.500000, -0.809017, -0.309017], 
				[ -0.262866, -0.951056, -0.162460], 
				[ -0.850651, -0.525731,  0.000000], 
				[ -0.716567, -0.681718, -0.147621], 
				[ -0.716567, -0.681718,  0.147621], 
				[ -0.525731, -0.850651,  0.000000], 
				[ -0.500000, -0.809017,  0.309017], 
				[ -0.238856, -0.864188,  0.442863], 
				[ -0.262866, -0.951056,  0.162460], 
				[ -0.864188, -0.442863,  0.238856], 
				[ -0.809017, -0.309017,  0.500000], 
				[ -0.688191, -0.587785,  0.425325], 
				[ -0.681718, -0.147621,  0.716567], 
				[ -0.442863, -0.238856,  0.864188], 
				[ -0.587785, -0.425325,  0.688191], 
				[ -0.309017, -0.500000,  0.809017], 
				[ -0.147621, -0.716567,  0.681718], 
				[ -0.425325, -0.688191,  0.587785], 
				[ -0.162460, -0.262866,  0.951056], 
				[  0.442863, -0.238856,  0.864188], 
				[  0.162460, -0.262866,  0.951056], 
				[  0.309017, -0.500000,  0.809017], 
				[  0.147621, -0.716567,  0.681718], 
				[  0.000000, -0.525731,  0.850651], 
				[  0.425325, -0.688191,  0.587785], 
				[  0.587785, -0.425325,  0.688191], 
				[  0.688191, -0.587785,  0.425325], 
				[ -0.955423,  0.295242,  0.000000], 
				[ -0.951056,  0.162460,  0.262866], 
				[ -1.000000,  0.000000,  0.000000], 
				[ -0.850651,  0.000000,  0.525731], 
				[ -0.955423, -0.295242,  0.000000], 
				[ -0.951056, -0.162460,  0.262866], 
				[ -0.864188,  0.442863, -0.238856], 
				[ -0.951056,  0.162460, -0.262866], 
				[ -0.809017,  0.309017, -0.500000], 
				[ -0.864188, -0.442863, -0.238856], 
				[ -0.951056, -0.162460, -0.262866], 
				[ -0.809017, -0.309017, -0.500000], 
				[ -0.681718,  0.147621, -0.716567], 
				[ -0.681718, -0.147621, -0.716567], 
				[ -0.850651,  0.000000, -0.525731], 
				[ -0.688191,  0.587785, -0.425325], 
				[ -0.587785,  0.425325, -0.688191], 
				[ -0.425325,  0.688191, -0.587785], 
				[ -0.425325, -0.688191, -0.587785], 
				[ -0.587785, -0.425325, -0.688191], 
				[ -0.688191, -0.587785, -0.425325]
			]
			
			def self.get_anorms_index(normal_x, normal_y, normal_z)
				#TODO three cycles, need to optimize
				
				best_normal = Anorms.find_all do |n|
					#same sign
					n[0] * normal_x >= 0 &&
					n[1] * normal_y >= 0 &&
					n[2] * normal_z >= 0 
				end.min_by do |n|					
					(n[0] - normal_x) * (n[0] - normal_x) +
					(n[1] - normal_y) * (n[1] - normal_y) +
					(n[2] - normal_z) * (n[2] - normal_z)
				end
				
				raise "couldn't get anorms_index for [#{normal_x}, #{normal_y}, #{normal_z}] " if best_normal.nil?
				
				Anorms.index best_normal
			end
		end
	end
end
